package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.CheckItemService;
import com.itheima.contant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckItemException;
import com.itheima.pojo.CheckItem;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkItem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    /*分页查询*/
    @RequestMapping("/pageQuery")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult pageQuery(@RequestBody QueryPageBean page){
        return checkItemService.pageQuery(page.getCurrentPage(),page.getPageSize(),page.getQueryString());
    }

    /*添加新的检查项*/
    @RequestMapping("/addCheckItem")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result addCheckItem(@RequestBody CheckItem checkItem){

        try {
            checkItemService.addCheckItem(checkItem);
            return Result.success(MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return Result.error(MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /*删除方法*/
    @RequestMapping("/deleteCheckItem")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result deleteCheckItem(Integer id){

        try {
            checkItemService.deleteCheckItem(id);
            return Result.success(MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (CheckItemException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /*点击编辑，数据回显*/
    @RequestMapping("/queryCheckItemById")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result queryCheckItemById(Integer id){
        try {
            CheckItem checkItem= checkItemService.queryCheckItemById(id);
            return Result.success(MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            return Result.error(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /*编辑，更改数据*/
    @RequestMapping("/updateCheckItem")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        try {
            checkItemService.updateCheckItem(checkItem);
            return Result.success(MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return Result.error(MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /*查询所有检查项*/
    @RequestMapping("/queryAllCheckItem")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result queryAllCheckItem(){
        try {
           List<CheckItem> checkItemList =  checkItemService.queryAllCheckItem();
            return Result.success("",checkItemList);
        } catch (Exception e) {
            return Result.error("");
        }
    }

}
